package com.duan.controller;

import com.duan.base.JSONResponse;
import com.duan.mapper.AwardMapper;
import com.duan.mapper.MemberMapper;
import com.duan.mapper.SettingMapper;
import com.duan.pojo.Award;
import com.duan.pojo.Member;
import com.duan.pojo.Setting;
import com.duan.pojo.User;
import com.duan.service.AwardService;
import com.duan.service.MemberService;
import com.duan.service.SettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Set;

@RestController
public class SettingController {

    private final SettingService settingService;

    private final AwardService awardService;

    private final MemberService memberService;

    @Autowired
    public SettingController(SettingService settingService, AwardService awardService, MemberService memberService){
        this.settingService = settingService;
        this.awardService = awardService;
        this.memberService = memberService;
    }

    /****
     * 进入抽奖设置页面，初始化的接口，返回页面所有需要的数据：
     * batchNumber
     * user（用户信息）
     * awards（奖项列表， 根据settingId查）
     * members（抽奖人员列表， 根据settingId查）
     * @param session 上下文，用来获取login后的currentUser信息
     * @return 将所有的信息封装到setting中一并返回
     */
    @GetMapping("/api/setting/query")
    public JSONResponse query(HttpServletResponse resp, HttpSession session){
        JSONResponse jsonResponse = new JSONResponse();

        //1. 获取session中的currentUser信息
        User user = (User)session.getAttribute("currentUser");

        //判断是否登录
        if(user == null){
            resp.setStatus(401);
            jsonResponse.setSuccess(false);
            jsonResponse.setCode("USR000");
            jsonResponse.setMessage("用户没有登录，不允许访问");

            return jsonResponse;
        }

        //2. 根据userId查询setting信息
        Setting setting = settingService.queryByUserId(user.getId());

        //3. 把user设置到setting新增的user属性中
        setting.setUser(user);

        //4. 根据settingId查询award列表，设置到setting新增属性awards中
        List<Award> awards = awardService.queryBySettingId(setting.getId());
        setting.setAwards(awards);

        //5. 根据setting_id查询member列表，设置setting新增属性到members中
        List<Member> members = memberService.queryBySettingId(setting.getId());
        setting.setMembers(members);

        jsonResponse.setSuccess(true);
        jsonResponse.setData(setting);

        return jsonResponse;
    }

    @GetMapping("/api/setting/update")
    public JSONResponse update(HttpSession session, Integer batchNumber, HttpServletResponse resp){
        User user = (User)session.getAttribute("currentUser");
        JSONResponse jsonResponse = new JSONResponse();

        //判断是否登录
        if(user == null){
            resp.setStatus(401);
            jsonResponse.setSuccess(false);
            jsonResponse.setCode("USR000");
            jsonResponse.setMessage("用户没有登录，不允许访问");

            return jsonResponse;
        }
        int n = settingService.update(batchNumber, user.getId());
        jsonResponse.setSuccess(true);
        return jsonResponse;
    }










}
